<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="所属区片：" prop="community_id">
                <el-select v-model="queryParams.community_id" placeholder="请选择区片" size="small"  >
                    <el-option v-for="(item, index) in communityList" :key="index" :label="item.title"
                               :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="水机名称" prop="search">
                <el-input
                    v-model="queryParams.search"
                    placeholder="请输入水机名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="状态" clearable>
                    <el-option
                        v-for="dict in dict.type.sys_normal_disable"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                >新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table  v-loading="loading" :data="masterLists" border fit highlight-current-row>
            <el-table-column type="selection" width="55" bo align="center" label-width="100px" />
            <el-table-column label="编号" align="center" prop="water_id"  width="50"/>
            >
            <el-table-column align="center" label="水机名称/编号">
                <template slot-scope="{row}">
                    {{row.water_title}}<br>
                    【{{row.code}}】
                </template>
            </el-table-column>
            <el-table-column align="center" label="所属片区">
                <template slot-scope="{row}">
                    {{ row.community? row.community.title : "" }}
                </template>
            </el-table-column>
            <el-table-column show-overflow-tooltip prop="address" label="水机地址" align="center"/>

            <el-table-column prop="status" align="center" label="状态" width="100">
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.sys_normal_disable" :value="row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
                <template slot-scope="{row}">
                    <el-dropdown size="mini" @command="(command) => handleCommand(command, row)" >
                    <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="detail" icon="el-icon-view">查看</el-dropdown-item>
                        <el-dropdown-item command="editBd" icon="el-icon-view">绑定设备</el-dropdown-item>
                        <el-dropdown-item command="delete" icon="el-icon-view">删除</el-dropdown-item>
                        <el-dropdown-item command="electricity_water" icon="el-icon-view">水/电费</el-dropdown-item>
                        <el-dropdown-item command="equipmentParams" icon="el-icon-view">参数设置</el-dropdown-item>
                        <el-dropdown-item command="property" icon="el-icon-view">查看消费记录</el-dropdown-item>
<!--                        <el-dropdown-item command="electricity" icon="el-icon-view">电费</el-dropdown-item>-->
<!--                        <el-dropdown-item command="water" icon="el-icon-view">水费</el-dropdown-item>-->
                    </el-dropdown-menu>
                    </el-dropdown>

                </template>
            </el-table-column>
        </el-table>
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.page"
            :limit.sync="queryParams.limit"
            @pagination="getList"
        />



        <!-- 绑定设备 对话框 -->
       <el-dialog :title="title" :visible.sync="open" width="700px"  >
            <el-form ref="form" :model="form" :rules="rules" label-width="200px">
                <el-form-item label="水机名称" prop="water_title">
                    <el-input v-model="form.water_title" disabled placeholder="请输入水机名称"/>
                </el-form-item>
                <el-form-item label="水机名称：" prop="mineral_title">
                    <el-input v-model="form.mineral_title" placeholder="请输入水机名称"/>
                </el-form-item>


                <el-form-item label="水机主板id：" prop="equipment_id">
                    <el-select v-model="form.equipment_id" filterable placeholder="请选择区片" size="small"  >
                        <el-option v-for="(item, index) in equipmentList" :key="index" :label="item.EquipmentName" :value="item.EquipmentID"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="主板编号：" prop="mid">
                    <el-select v-model="form.mid" filterable placeholder="请选择区片" size="small"  >
                        <el-option v-for="(item, index) in equipmentList" :key="index" :label="item.EquipmentName" :value="item.MID"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="水机名称[富氢水]" prop="rich_title">
                    <el-input v-model="form.rich_title" placeholder="请输入水机名称【富氢水】"/>
                </el-form-item>

                <el-form-item label="水机主板id[富氢水]：" prop="equipment_id_rich">
                    <el-select v-model="form.equipment_id_rich" filterable placeholder="请选择区片" size="small"  >
                        <el-option v-for="(item, index) in equipmentList" :key="index" :label="item.EquipmentName"
                                   :value="item.EquipmentID"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="主板编号[富氢水]：" prop="mid_rich">
                    <el-select v-model="form.mid_rich" filterable placeholder="请选择区片" size="small"  >
                        <el-option v-for="(item, index) in equipmentList" :key="index" :label="item.EquipmentName"
                                   :value="item.MID"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>


    </div>
</template>
<script>


import { getAllCom} from "@/api/grzq/community.js";
import {getApparatusList,getApparatusJundaAllList,updateBdJdApparatus}from "@/api/grzq/apparatus.js";

import {removeFrom} from "@/utils"; //引入AMapLoader

export default {
    name: "Apparatus",
    dicts:['sys_normal_disable'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 表格数据
            masterLists: [],
            communityList: [],
            apparatusList: [],
            total: 0,
            // 弹出层标题
            title: "绑定设备",
            // 是否显示弹出层
            open: false,
            // 是否展开，默认全部展开
            isExpandAll: true,
            // 重新渲染表格状态
            refreshTable: true,
            // 查询参数
            queryParams: {
                search: undefined,
                community_id: undefined,
                type: undefined,
                apparatus_id: undefined,
                status: undefined,
                page: 1,
                limit: 10,
            },
            equipmentList:[],
            form:{},
            rules:{
                mineral_title: [
                    { required: true, message: '站点名称【-骏达系统】不能为空', }
                ],
                equipment_id: [
                    { required: true, message: '请选择水机ID【-骏达系统】不能为空'}
                ],
                mid: [
                    { required: true, message: '请选择水机主板【-骏达系统】不能为空' }
                ],
                rich_title: [
                    { required: true, message: '水机名称【富氢水-骏达系统】不能为空'}
                ],
                equipment_id_rich: [
                        { required: true, message: '请选择水机名称【富氢水】-骏达系统 '}
                ],
                mid_rich: [
                        { required: true, message: '请选择水机主板【富氢水】-骏达系统 '}
                ]
            },

        };
    },
    created() {
        this.getAllCom();
        this.getList();
    },
    methods: {
        /** 查询部门列表 */
        async getList() {
            let that = this;
            that.loading = true;
            const {data, status, msg} = await getApparatusList(this.queryParams);
            that.masterLists = data.data;
            that.loading = false;
            that.total = data.total;
            that.queryParams.limit = data.per_page || 10;

        },
        //片区
        async getAllCom() {
            const res = await getAllCom();
            this.communityList = res.data;
        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                water_id: undefined,
                water_title: undefined,
                equipment_id: undefined,
                equipment_id_rich: undefined,
                mid: undefined,
                mid_rich: undefined,
                mineral_title: undefined,
                rich_title: undefined,
            };
            this.resetForm("form");
        },

        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd(row) {
            this.reset();
            this.open = true;
            this.title = "添加数据";
            this.$router.push({
                'path':`/comm/waterdetail`,
                'query': {id:0},
            });
        },
        // 更多操作触发
        handleCommand(command, row) {
            var query = {};
            if (row.water_id) query.id = row.water_id;
            switch (command) {
                case "detail":
                    this.$router.push({
                        'path':`/comm/waterdetail`,
                        'query': query,
                    });
                    break;
                case "editBd":
                    this.getApparatusJundaList()
                    this.open = true
                    this.form = row
                    this.$forceUpdate()
                    console.log("row",row)
                    this.form.mineral_title = row.mineral_title?row.mineral_title:row.water_title
                    this.form.rich_title = row.rich_title?row.rich_title:row.water_title+'富氢水'

                    console.log("this",this.form)
                    break;
                case "electricity_water":
                    this.handleElectricityWater(row);
                    this.$router.push({
                        'path':`/comm/hydropower`,
                        'query': query,
                    });
                    break;
                case "equipmentParams":
                    this.$router.push({
                        'path':`/comm/equipmentParams`,
                        'query': query,
                    });
                    break;
                default:
                    break;
            }
        },


        async getApparatusJundaList() {
            const {data} = await getApparatusJundaAllList();
            this.equipmentList = data
        },
        handleElectricityWater(row){

        },
        submitForm: function() {
            // 绑定设备信息
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.water_id != undefined) {
                        const jdform ={
                            water_id : this.form.water_id,
                            water_title : this.form.water_title,
                            equipment_id : this.form.equipment_id,
                            equipment_id_rich : this.form.equipment_id_rich,
                            mid : this.form.mid,
                            mid_rich : this.form.mid_rich,
                            mineral_title : this.form.mineral_title,
                            rich_title : this.form.rich_title,
                        };

                        updateBdJdApparatus(jdform).then(response => {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        })
                    } else {

                    }
                }
            })
        },

    }
};
</script>
